package com.zd.common.common.utils;

import java.math.BigInteger;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.zd.common.common.utils.decode.Base64;

/**
 * 加密
 *
 * @author lishi
 * @date 2017-12-18 上午11:28:15
 */
public class EncryptUtil {

	public static final String KEY_ALGORITHM = "AES";
	public static final String CIPER_ALGORITHM = "AES";

	/**
	 * 加密
	 *
	 * @param content
	 *            需要加密的内容
	 * @param password
	 *            加密密码
	 * @return
	 */
	public static String encrypt(String content, String password) throws Exception {
		SecretKeySpec key = new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);

		Cipher cipher = Cipher.getInstance(CIPER_ALGORITHM);// 创建密码器
		byte[] byteContent = content.getBytes("utf-8");
		cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
		byte[] result = cipher.doFinal(byteContent);
		return parseByte2HexStr(result);
	}

	public static String encrypt(String contentd) throws Exception {
		return encrypt(contentd);
	}
	
	

	/**
	 * 解密
	 *
	 * @param str
	 *            待解密内容
	 * @param password
	 *            解密密钥
	 * @return
	 */
	public static String decrypt(String str, String password) throws Exception {
		byte[] content = parseHexStr2Byte(str);
		SecretKeySpec key = new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);

		Cipher cipher = Cipher.getInstance(CIPER_ALGORITHM);// 创建密码器
		cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
		byte[] result = cipher.doFinal(content);
		return new String(result, "UTF-8");
	}

	// public static String decrypt(String string) throws Exception {
	// return decrypt(string, CommonConstatns.AES_KEY);
	// }

	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	public static String getMD5(String str) {
		try {
			// 生成一个MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 计算md5函数
			md.update(str.getBytes());
			// digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
			// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
			return new BigInteger(1, md.digest()).toString(16);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 对字符串md5加密(大写+数字)
	 * 
	 * @param str
	 *            传入要加密的字符串
	 * @return MD5加密后的字符串
	 */

	public static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	 public static String enBase64(String text)throws Exception{
	 return new String(Base64.encodeBase64(text.getBytes("UTF-8")),"UTF-8");
	 }
	
	 public static String deBase64(String text)throws Exception{
	 return new String(Base64.decodeBase64(text.getBytes("UTF-8")),"UTF-8");
	 }
	 
	 
	 public static void main(String[] args) throws Exception {
		String pwd = "fb18fc197e644d38";
		String text = "FA1EE307F7D5BF4EDED94E56E1FF163A";
//		String encryptStr = encrypt(text, pwd);
//		System.out.println(encryptStr);
		String decryptStr = decrypt(text, pwd);
		System.out.println(decryptStr);
	}
}
